Telegram Group & Telegram Channel
Наверное, самая распространённая ошибка новичков в Python — это использование изменяемого объекта в качестве значения по умолчанию для аргумента функции. Такой объект будет общим для всех вызовов функции, что может привести к неожиданным результатам:


def append_length(lst=[]):
lst.append(len(lst))
return lst

print(append_length([1, 2])) # [1, 2, 2]
print(append_length()) # [0]
print(append_length()) # [0, 1]


Однако в некоторых случаях, например при реализации кешей, такое поведение может быть полезным:


def fact(x, cache={0: 1}):
if x not in cache:
cache[x] = x * fact(x - 1)
return cache[x]

print(fact(5))


В этом примере мы сохраняем вычисленные значения факториала внутри значения аргумента по умолчанию. Причём к этому значению даже можно обратиться напрямую:


>>> fact.__defaults__
({0: 1, 1: 1, 2: 2, 3: 6, 4: 24, 5: 120},)


👉@BookPython



tg-me.com/BookPython/3670
Create:
Last Update:

Наверное, самая распространённая ошибка новичков в Python — это использование изменяемого объекта в качестве значения по умолчанию для аргумента функции. Такой объект будет общим для всех вызовов функции, что может привести к неожиданным результатам:


def append_length(lst=[]):
lst.append(len(lst))
return lst

print(append_length([1, 2])) # [1, 2, 2]
print(append_length()) # [0]
print(append_length()) # [0, 1]


Однако в некоторых случаях, например при реализации кешей, такое поведение может быть полезным:


def fact(x, cache={0: 1}):
if x not in cache:
cache[x] = x * fact(x - 1)
return cache[x]

print(fact(5))


В этом примере мы сохраняем вычисленные значения факториала внутри значения аргумента по умолчанию. Причём к этому значению даже можно обратиться напрямую:


>>> fact.__defaults__
({0: 1, 1: 1, 2: 2, 3: 6, 4: 24, 5: 120},)


👉@BookPython

BY Библиотека Python разработчика | Книги по питону


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/BookPython/3670

View MORE
Open in Telegram


Библиотека Python разработчика Telegram | DID YOU KNOW?

Date: |

At a time when the Indian stock market is peaking and has rallied immensely compared to global markets, there are companies that have not performed in the last 10 years. These are definitely a minor portion of the market considering there are hundreds of stocks that have turned multibagger since 2020. What went wrong with these stocks? Reasons vary from corporate governance, sectoral weakness, company specific and so on. But the more important question is, are these stocks worth buying?

That strategy is the acquisition of a value-priced company by a growth company. Using the growth company's higher-priced stock for the acquisition can produce outsized revenue and earnings growth. Even better is the use of cash, particularly in a growth period when financial aggressiveness is accepted and even positively viewed.he key public rationale behind this strategy is synergy - the 1+1=3 view. In many cases, synergy does occur and is valuable. However, in other cases, particularly as the strategy gains popularity, it doesn't. Joining two different organizations, workforces and cultures is a challenge. Simply putting two separate organizations together necessarily creates disruptions and conflicts that can undermine both operations.

Библиотека Python разработчика from tw


Telegram Библиотека Python разработчика | Книги по питону
FROM USA